package com.yanqu.road.server.manger.activity.witchdraw;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.witchdraw.config.WitchDrawCombConfig;
import com.yanqu.road.entity.activity.witchdraw.config.WitchDrawConfig;
import com.yanqu.road.entity.activity.witchdraw.config.WitchDrawItemConfig;
import com.yanqu.road.entity.activity.witchdraw.data.WitchDrawUserData;
import com.yanqu.road.entity.enums.activity.witchdraw.eWitchDrawSummonType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.WitchDrawBusiness;
import com.yanqu.road.logic.helper.WeightHelper;
import com.yanqu.road.pb.activity.WitchDrawProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.witchDraw.WitchDrawModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class WitchDrawMgr extends TempMgr {

    private static WitchDrawConfig config;

    private static ActivityInfo activityInfo;

    public static Random random = new Random();

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static WitchDrawConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static void setActivityInfo(ActivityInfo activityInfo) {
        WitchDrawMgr.activityInfo = activityInfo;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData(){
        getLogger().info("reload witchDraw activity start");
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.WitchDraw.getValue());
        if (activityInfoList.size() == 0){
            getLogger().info("reload witchDraw activity finish, no activity in show time.");
            config = null;
            activityInfo = null;
            return;
        }
        activityInfo = activityInfoList.get(0);
        //活动配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        Map<Integer, WitchDrawCombConfig> witchDrawCombConfigMap = WitchDrawBusiness.getWitchDrawCombConfigMap(activityInfo.getActivityId());
        Map<Integer, WitchDrawItemConfig> witchDrawItemConfigMap = WitchDrawBusiness.getWitchDrawItemConfigMap(activityInfo.getActivityId());

        WitchDrawConfig witchDrawConfig = new WitchDrawConfig(witchDrawCombConfigMap,witchDrawItemConfigMap,activityConfigMap);
        config = witchDrawConfig;

        getLogger().info("reload witchDraw activity end activityId = {}",activityInfo.getActivityId());
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(WitchDrawModule.class).syncConfig();
            player.getModule(WitchDrawModule.class).syncUserData();
        }
    }


    public static WitchDrawProto.WitchDrawConfigSync.Builder getConfigMsg(String language){

        if (activityInfo == null){
            return null;
        }
        if (config == null){
            return null;
        }

        WitchDrawProto.WitchDrawConfigSync.Builder builder = WitchDrawProto.WitchDrawConfigSync.newBuilder();
        Map<Integer, WitchDrawCombConfig> combConfigMap = config.getCombConfigMap();
        for (WitchDrawCombConfig witchDrawCombConfig : combConfigMap.values()){
            WitchDrawProto.WitchDrawCombConfig.Builder combBuilder = WitchDrawProto.WitchDrawCombConfig.newBuilder();
            combBuilder.setId(witchDrawCombConfig.getId());
            combBuilder.setType(witchDrawCombConfig.getType());
            combBuilder.setReward(witchDrawCombConfig.getReward());
            combBuilder.setCombParam(witchDrawCombConfig.getCombParam());
            combBuilder.setWeight(witchDrawCombConfig.getWeight());
            combBuilder.setShowWeight(witchDrawCombConfig.getShowWeight());
            builder.addCombConfig(combBuilder);
        }
        Map<Integer, WitchDrawItemConfig> drawItemConfigMap = config.getDrawItemConfigMap();

        for (WitchDrawItemConfig itemConfig:drawItemConfigMap.values()){
            WitchDrawProto.WitchDrawItemConfig.Builder itemBuilder = WitchDrawProto.WitchDrawItemConfig.newBuilder();
            itemBuilder.setId(itemConfig.getId());
            itemBuilder.setName(ServerLanguageMgr.getContent(itemConfig.getName(),language));
            itemBuilder.setDesc(ServerLanguageMgr.getContent(itemConfig.getDesc(),language));
            builder.addItemConfig(itemBuilder);
        }
        return builder;

    }

    /**
     * 获取随机的无双保底次数
     * @return
     */
    public static int getRandomSimpleEnsureTimes() {
        if (config == null) {
            return -1;
        }
        int drawSimpleEnsureSmallestParam = config.getDrawSimpleEnsureSmallestParam();
        int drawSimpleEnsureBiggestParam = config.getDrawSimpleEnsureBiggestParam();
        if (drawSimpleEnsureSmallestParam <= 0 || drawSimpleEnsureBiggestParam <= 0 || drawSimpleEnsureSmallestParam > drawSimpleEnsureBiggestParam){
            return -1;
        }
        if (drawSimpleEnsureSmallestParam == drawSimpleEnsureBiggestParam){
            return drawSimpleEnsureSmallestParam;
        }
        // 生成随机数 从1开始
        int randomNum = random.nextInt(drawSimpleEnsureBiggestParam - drawSimpleEnsureSmallestParam + 1) + drawSimpleEnsureSmallestParam;

        return randomNum;
    }

    /**
     * 获取随机的神话保底次数
     */
    public static int getRandomMythEnsureTimes() {
        if (config == null) {
            return -1;
        }
        int drawMythEnsureSmallestParam = config.getDrawMythEnsureSmallestParam();
        int drawMythEnsureBiggestParam = config.getDrawMythEnsureBiggestParam();
        if (drawMythEnsureSmallestParam <= 0 || drawMythEnsureBiggestParam <= 0 || drawMythEnsureSmallestParam > drawMythEnsureBiggestParam){
            return -1;
        }
        if (drawMythEnsureSmallestParam == drawMythEnsureBiggestParam){
            return drawMythEnsureSmallestParam;
        }
        // 生成随机数 从1开始
        int randomNum = random.nextInt(drawMythEnsureBiggestParam - drawMythEnsureSmallestParam + 1) + drawMythEnsureSmallestParam;

        return randomNum;
    }

    /**
     * 根据抽奖combId与当前抽数确定某一个comb的浮动权重 没有浮动返回-1
     */
    public static int getFloatDrawCombWeight(int combId, int drawTime ,boolean canMyth){
        if (config == null) {
            return -1;
        }
        Map<Integer, Map<Integer, Integer>> floatWeightMap = config.getFloatWeightMap();
        if (!floatWeightMap.containsKey(combId)){
            return -1;
        }
        //拿出这个comb的配置
        WitchDrawCombConfig witchDrawCombConfig = config.getCombConfigMap().get(combId);
        if (witchDrawCombConfig ==null){
            return -1;
        }
        //如果不能是神话 并且这个comb是神话的话 概率这里直接返回0
        if (!canMyth && witchDrawCombConfig.getType() == eWitchDrawSummonType.mythical.getValue()){
            return 0;
        }

        Map<Integer, Integer> combFloatWeightMap = floatWeightMap.get(combId);
        int nowStepLevel = 0;
        for (int drawNum:combFloatWeightMap.keySet()){
            if (drawTime >= drawNum && drawNum > nowStepLevel){
                nowStepLevel = drawNum;
            }
        }
        if (combFloatWeightMap.containsKey(nowStepLevel)){
            return combFloatWeightMap.get(nowStepLevel);
        }else {
            return -1;
        }
    }


    /**
     * 获取随机一个抽奖结果组合
     */
    public static WitchDrawCombConfig randomDrawCombConfig(int drawTime,boolean canMyth){
        if (config == null) {
            return null;
        }
        Map<Integer, Integer> drawCombWeightMap = new ConcurrentHashMap<>(config.getDrawCombWeightMap());
        if (drawCombWeightMap.isEmpty()){
            return null;
        }
        //遍历所有组合权重 查看是否有随次数浮动
        for (int combId:drawCombWeightMap.keySet()){
            int floatDrawCombWeight = getFloatDrawCombWeight(combId, drawTime,canMyth);
            if (floatDrawCombWeight >= 0){
                drawCombWeightMap.put(combId,floatDrawCombWeight);
            }
        }

        int randomValue = WeightHelper.getRandomValue(drawCombWeightMap);
        return config.getCombConfigMap().get(randomValue);
    }

    /**
     * 获取大保底 每次活动一次的保底
     */
    public static WitchDrawCombConfig getMythEnsureCombConfig(){
        if (config == null) {
            return null;
        }
        for (WitchDrawCombConfig drawCombConfig:config.getCombConfigMap().values()){
            if (drawCombConfig.getType() == eWitchDrawSummonType.mythical.getValue()){
                return drawCombConfig;
            }
        }
        return null;
    }

    /**
     * 获取小保底 每次获取后更新的保底
     */
    public static WitchDrawCombConfig getSimpleEnsureCombConfig(){
        if (config == null) {
            return null;
        }
        for (WitchDrawCombConfig drawCombConfig:config.getCombConfigMap().values()){
            if (drawCombConfig.getType() == eWitchDrawSummonType.unparalleled.getValue()){
                return drawCombConfig;
            }
        }
        return null;
    }

    /**
     * 检测是否可以抽出神话
     */
    public static boolean canDrawMyth(WitchDrawUserData userData){
        //如果配置为空
        if (config == null){
            getLogger().error("witch Draw canDrawMyth config is null");
            return false;
        }
        //如果用户数据为空
        if (userData == null){
            return false;
        }
        //如果大于这个积分 就不限制他白嫖了
        if (userData.getScore() > config.getDrawMythLimitMythDrawScore()){
            return true;
        }
        //如果玩家获得的神话次数已经超出限制了..不给他抽
        if (userData.getMythTotalDrawTimes() >= config.getDrawMythLimitMythDrawTimes()){
            return false;
        }
        //其他情况 还是能出的吧
        return true;
    }





}
